package com.tanhua.dubbo.api;

import com.tanhua.domain.mongo.*;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@Service
public class SoulTestApiImpl implements SoulTestApi {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public List<SoulTest> findSoulTestList(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        List<SoulTest> soulTestList = mongoTemplate.find(query, SoulTest.class);
        return soulTestList;
    }

    @Override
    public List<SoulTestQuestion> findQuestionList(String sid) {
        Query query = new Query(Criteria.where("sid").is(sid));
        List<SoulTestQuestion> questions = mongoTemplate.find(query, SoulTestQuestion.class);
        return questions;
    }

    @Override
    public List<Option> findOptions(String questionQid, String sid) {
        Query query = new Query(Criteria.where("qid").is(questionQid).and("sid").is(sid))
                .with(Sort.by(Sort.Order.asc("created")));
        List<Option> options = mongoTemplate.find(query, Option.class);
        return options;
    }

    @Override
    public void updateRecordReport(RecordReport recordReport, Integer score) {
        Query query = new Query(Criteria.where("id").is(recordReport.getId()));
        Update update = new Update().set("countScore",score);
        mongoTemplate.updateMulti(query,update,RecordReport.class);
    }

    @Override
    public SoulTestQuestion findQid(String qid) {
        Query query = new Query(Criteria.where("qid").is(qid));
        SoulTestQuestion soulTestQuestion = mongoTemplate.findOne(query, SoulTestQuestion.class);
        return soulTestQuestion;
    }

    @Override
    public Option findOid(String oid) {
        Query query = new Query(Criteria.where("oid").is(oid));
        return mongoTemplate.findOne(query, Option.class);
    }

    @Override
    public RecordReport findCountScore(Long userId, String sid) {
        Query query = new Query(Criteria.where("userId").is(userId).and("sid").is(sid));
        return mongoTemplate.findOne(query, RecordReport.class);
    }

    @Override
    public void saveRecordReport(RecordReport recordReport) {
        mongoTemplate.save(recordReport);
    }

    @Override
    public RecordQuestion saveRecordQuestion(RecordQuestion recordQuestion) {
        RecordQuestion question = mongoTemplate.save(recordQuestion);
        return question;
    }

    @Override
    public RecordReport findByRid(String reportId) {
        Query query = new Query(Criteria.where("reportId").is(reportId));
        RecordReport recordReport = mongoTemplate.findOne(query, RecordReport.class);
        return recordReport;
    }

    @Override
    public ScoringReport findByCountRid(String i) {
        Query query = new Query(Criteria.where("srid").is(i));
        ScoringReport scoringReport = mongoTemplate.findOne(query, ScoringReport.class);
        return scoringReport;
    }

    @Override
    public void updateReportId(String reportId, Long userId, String sid) {
        Query query = new Query(Criteria.where("userId").is(userId).and("sid").is(sid));
        Update update = new Update().set("reportId",reportId);
        mongoTemplate.updateMulti(query,update,SoulTest.class);
    }

    @Override
    public SoulTest findNextLevelByUidAndSid(Long userId, String sidPlus) {
        Query query = new Query(Criteria.where("userId").is(userId).and("sid").is(sidPlus));
        SoulTest one = mongoTemplate.findOne(query, SoulTest.class);
        return one;
    }

    @Override
    public void updateSoulTestIsLock(Long userId, String sidPlus) {
        Query query = new Query(Criteria.where("userId").is(userId).and("sid").is(sidPlus));
        Update update = new Update().set("isLock",0);
        mongoTemplate.updateMulti(query,update,SoulTest.class);
    }

    @Override
    public Option findOptionByOqId(String qid, String oid) {
        Query query = new Query(Criteria.where("qid").is(qid).and("oid").is(oid));
        Option one = mongoTemplate.findOne(query, Option.class);
        return one;
    }

    @Override
    public void saveSoulTestUser(Long userId) {

    }
}
